package com.hxgis.web.controller;

import com.hxgis.common.base.ResultVO;
import com.hxgis.common.base.StationCache;
import com.hxgis.common.entity.Station;
import com.hxgis.common.entity.StationExtremeInfo;
import com.hxgis.common.enums.PreYear30Enum;
import com.hxgis.common.service.*;
import com.hxgis.common.utils.RectField;
import com.hxgis.common.utils.ResultVOUtil;
import com.hxgis.common.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("rain/day")
@Api(tags = "RainDayController")
public class RainDayController {

    @Autowired
    private RainDayService rainDayService;

    @Autowired
    private StationService stationService;

    @Autowired
    ProcRainInfoService procRainInfoService;

    @Autowired
    ExtremeProcService extremeProcService;

    @Autowired
    private RainExtremeInfoService extremeInfoService;

    @Autowired
    private StationExtremeService stationExtremeService;


    /**
     * 从cimiss将日降雨事件导入日降水表
     * @param startYear
     * @param endYear
     * @return
     */
    @ApiOperation(value="从cimiss将日降雨事件导入日降水表按照年批量", notes="startYear：1951，endYear：2019")
    @GetMapping("toEs")
    public ResultVO addToRainEs(@RequestParam("startYear") String startYear,@RequestParam("endYear") String endYear) throws ParseException {
        //从es查询所有的站点
        List<Station> stations = stationService.selectAll();
        List<String> stationIds = new ArrayList<>();
        stations.stream().forEach(x -> {
            stationIds.add(x.getStationNo());
        });

        for (int i = Integer.valueOf(startYear); i <= Integer.valueOf(endYear); i++) {

            long s = System.currentTimeMillis();
            //第一步将数据存入rainday
            for (int j = 1; j <= 12; j++) {
                String firstDayOfMonth = TimeUtil.getFirstDayOfMonth(i, j);
                String lastDayOfMonth = TimeUtil.getLastDayOfMonth(i, j);
//                List<String> monthBetweenDateStr = TimeUtil.getMonthBetweenDateStr(TimeUtil.preDay(firstDayOfMonth,1), TimeUtil.nextDay(lastDayOfMonth,1));
//                monthBetweenDateStr.stream().forEach(x -> {
//                    log.info("当前正保存的年月是:{}",x);
//                    rainDayService.addToRainEs(x + "000000",stationIds);
//                });
                rainDayService.addToRainEs(TimeUtil.preDay(firstDayOfMonth,1), TimeUtil.nextDay(lastDayOfMonth,1), stationIds);
                log.info("{}到{}-----cimiss拉取元数据", TimeUtil.preDay(firstDayOfMonth,1), TimeUtil.nextDay(lastDayOfMonth,1));
            }
        }
        return ResultVOUtil.success();
    }


    @ApiOperation(value="从cimiss将日降雨事件导入日降水表按照时间，最好跨度不要太大", notes="startTime：19511231，endTime：20190101")
    @GetMapping("toEsByTime")
    public ResultVO toEsByTime(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) throws ParseException {
        //从es查询所有的站点
        List<Station> stations = stationService.selectAll();
        List<String> stationIds = new ArrayList<>();
        stations.stream().forEach(x -> {
            stationIds.add(x.getStationNo());
        });
//        List<String> monthBetweenDateStr = TimeUtil.getMonthBetweenDateStr(TimeUtil.preDay(startTime,1), TimeUtil.nextDay(endTime,1));
//        monthBetweenDateStr.stream().forEach(x -> {
//            log.info("当前正保存的年月是:{}",x);
//            rainDayService.addToRainEs(x + "000000",stationIds);
//        });
        rainDayService.addToRainEs(TimeUtil.preDay(startTime,1), TimeUtil.nextDay(endTime,1), stationIds);
        log.info("{}到{}-----cimiss拉取元数据", TimeUtil.preDay(startTime,1), TimeUtil.nextDay(endTime,1));
        return ResultVOUtil.success();
    }

    @ApiOperation(value="按照年补全多日降水量极值排位数据", notes="startYear：1951，endYear：2019")
    @GetMapping("completionRainDayByYear")
    public ResultVO completionRainDayByYear(@RequestParam("startYear") String startYear,@RequestParam("endYear") String endYear) throws ParseException {
        List<Station> stations = StationCache.getStations();
        for (int i = Integer.valueOf(startYear); i <= Integer.valueOf(endYear); i++) {
            //第一步将数据存入rainday
            for (int j = 1; j <= 12; j++) {
                String firstDayOfMonth = TimeUtil.getFirstDayOfMonth(i, j);
                String lastDayOfMonth = TimeUtil.getLastDayOfMonth(i, j);
                rainDayService.completionRainDay20(firstDayOfMonth, lastDayOfMonth, RectField.getBeanFields(stations,"stationNo"));
                rainDayService.completionRainDay08(firstDayOfMonth, lastDayOfMonth, RectField.getBeanFields(stations,"stationNo"));
            }
        }
        return ResultVOUtil.success();
    }


    @ApiOperation(value="按照任意时段最好跨度不要太大补全多日降水量极值排位数据", notes="startTime：19511231，endTime：20190101")
    @GetMapping("completionRainDay")
    public ResultVO completionRainDay(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
        List<Station> stations = StationCache.getStations();
        rainDayService.completionRainDay20(startTime, endTime, RectField.getBeanFields(stations,"stationNo"));
        rainDayService.completionRainDay08(startTime, endTime, RectField.getBeanFields(stations,"stationNo"));

        return ResultVOUtil.success();
    }




    @ApiOperation(value="按照年补全多日降水量极值连续降雨和连续无降雨数据，先要把连续日的降水数据入完才能跑极值的过程", notes="startYear：1951，endYear：2019")
    @GetMapping("completionContinueByYear")
    public ResultVO completionContinueByYear(@RequestParam("startYear") String startYear,@RequestParam("endYear") String endYear) throws ParseException {
//        List<Station> stations = StationCache.getStations();
//        for (int i = Integer.valueOf(startYear); i <= Integer.valueOf(endYear); i++) {
//            for (int j = 1; j <= 12; j++) {
//                String firstDayOfMonth = TimeUtil.getFirstDayOfMonth(i, j);
//                String lastDayOfMonth = TimeUtil.getLastDayOfMonth(i, j);
//                rainDayService.completionContinueByYear20(firstDayOfMonth, lastDayOfMonth, stations);
//                rainDayService.completionNotContinueByYear20(firstDayOfMonth, lastDayOfMonth, stations);
//                rainDayService.completionContinueByYear08(firstDayOfMonth, lastDayOfMonth, stations);
//                rainDayService.completionNotContinueByYear08(firstDayOfMonth, lastDayOfMonth, stations);
//            }
//        }
        rainDayService.completionContinueByYear20New(startYear + "0101",endYear + "1231");
        rainDayService.completionNotContinueByYear20New(startYear + "0101",endYear + "1231");
        rainDayService.completionContinueByYear08New(startYear + "0101",endYear + "1231");
        rainDayService.completionNotContinueByYear08New(startYear + "0101",endYear + "1231");
        return ResultVOUtil.success();
    }


    @ApiOperation(value="按照任意时段最好跨度不要太大补全多日降水量极值连续降雨和连续无降雨数据", notes="startTime：19511231，endTime：20190101")
    @GetMapping("completionContinueByDay")
    public ResultVO completionContinueByDay(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime) {
//        List<Station> stations = StationCache.getStations();
//        rainDayService.completionContinueByYear20(startTime, endTime, stations);
//        rainDayService.completionNotContinueByYear20(startTime, endTime, stations);
//        rainDayService.completionContinueByYear08(startTime, endTime, stations);
//        rainDayService.completionNotContinueByYear08(startTime, endTime, stations);

        rainDayService.completionContinueByYear20New(startTime,endTime);
        rainDayService.completionNotContinueByYear20New(startTime,endTime);
        rainDayService.completionContinueByYear08New(startTime,endTime);
        rainDayService.completionNotContinueByYear08New(startTime,endTime);
        return ResultVOUtil.success();
    }



    @ApiOperation(value="初始化站点阈值信息需要跑完降水表的连续日 连续降水连续无降水", notes="初始化站点阈值信息")
    @GetMapping("initExtremeInfoToEs")
    public ResultVO initExtremeInfoToEs(){
        //todo 这里要提供一个删除的 到20210101就要计算了 极端的数据需要重新跑



        stationExtremeService.saveExtremeInfo(PreYear30Enum.PRE_YEAR_30_START.getYear() + "0101",
                PreYear30Enum.PRE_YEAR_30_END.getYear() + "1231", RectField.getBeanFields(StationCache.getStations(),"stationNo"));

        return ResultVOUtil.success();
    }


    @ApiOperation(value="初始化极端事件相关数据(按照年)，连续1-15日降水极端数据", notes="startYear:2000  endYear:2019")
    @GetMapping("initExtremeInfo")
    public ResultVO initExtremeInfoByYear(@RequestParam("startYear") Integer startYear,
                                          @RequestParam("endYear") Integer endYear){
        List<StationExtremeInfo> allExtreInfo = stationExtremeService.getAllExtreInfo();
        for (int i = startYear; i <= endYear; i++) {
            for (int j = 1; j <= 12; j++) {
                String firstDayOfMonth = TimeUtil.getFirstDayOfMonth(i, j);
                String lastDayOfMonth = TimeUtil.getLastDayOfMonth(i, j);
                extremeInfoService.saveExtremeInfo(firstDayOfMonth, lastDayOfMonth, allExtreInfo);
            }
        }
        return ResultVOUtil.success();
    }


    @ApiOperation(value="初始化极端事件相关数据(按照时间，最好跨度不要太大)", notes="startTime：19511231，endTime：20190101")
    @GetMapping("initExtremeInfoByTime")
    public ResultVO initExtremeInfoByTime(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        List<StationExtremeInfo> allExtreInfo = stationExtremeService.getAllExtreInfo();
        extremeInfoService.saveExtremeInfo(startTime, endTime, allExtreInfo);
        return ResultVOUtil.success();
    }

    /**
     * 初始化过程数据  todo 从降水表中筛选出过程数据 方便计算到极端过程表
     * @return
     */
    @ApiOperation(value="初始化降水过程数据，必须等到降水数据全部完成", notes="startYear:2000  endYear:2019")
    @GetMapping("/procs")
    public ResultVO intProcInfos(@RequestParam("startYear") Integer startYear,
                                      @RequestParam("endYear") Integer endYear){
        for (int i = startYear; i <= endYear; i++) {
            procRainInfoService.saveProRainInfoByTime(i+"0101", i+"1231" );
        }

        log.info("初始化过程数据");


        return ResultVOUtil.success();
    }

    /**
     * 初始化任意时段过程数据
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value="最好跨度不要太大,初始化过程数据", notes="startYear:20000101  endYear:20000101")
    @GetMapping("/procs/byTime")
    public ResultVO intProcInfosByTime(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        procRainInfoService.saveProRainInfoByTime(startTime, endTime);
        log.info("初始化过程数据");
        return ResultVOUtil.success();
    }


    /**
     * 初始化任意时段过程极端数据
     * @param startTime
     * @param endTime
     * @return
     */
    @ApiOperation(value="最好跨度不要太大,初始化极端过程数据", notes="startTime:2000  endTime:2019")
    @GetMapping("/proc/extreme/byTime")
    public ResultVO procExtremeByTime(@RequestParam("startTime") String startTime,@RequestParam("endTime") String endTime){
        List<StationExtremeInfo> allExtreInfo = stationExtremeService.getAllExtreInfo();
        extremeProcService.saveExtremeProcByTime(startTime, endTime, allExtreInfo);
        log.info("初始化极端过程数据");
        return ResultVOUtil.success();
    }

    /**
     * 初始化过程数据
     * @return
     */
    @ApiOperation(value="初始化极端过程数据，通过年份", notes="startYear:2000  endYear:2019")
    @GetMapping("/proc/extreme/byYear")
    public ResultVO procExtremeByYear(@RequestParam("startYear") Integer startYear,
                                 @RequestParam("endYear") Integer endYear){
        List<StationExtremeInfo> allExtreInfo = stationExtremeService.getAllExtreInfo();
        for (int i = startYear; i <= endYear; i++) {
            extremeProcService.saveExtremeProcByTime(i+"0101", i+"1231", allExtreInfo);
        }
        log.info("初始化极端过程数据完成");
        return ResultVOUtil.success();
    }

}
